<?php
namespace app\admin\controller\user;

use app\admin\model\openservice\Openapps;
use app\admin\model\user\User;
use app\common\controller\Admin;
use app\common\annotation\NodeAnnotation;
use app\common\traits\ControllerCurd;
use think\Request;

/**
 * @NodeAnnotation(
 *     title="api用户管理",
 *     parent="admin/user.index",
 *     is_menu=0,
 *     is_view=0,
 *     auth=1
 * )
 */
class Apiuser extends Admin
{
    use ControllerCurd;

    public $model;

    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\user\ApiUser();
    }

    /**
     * @NodeAnnotation(
     *     title="Api用户",
     *     parent="admin/user.apiuser",
     *     is_menu=1,
     *     is_view=1,
     *     auth=1,
     *     MenuRoot="admin/user.index"
     * )
     */
    public function index(Request $request)
    {
        $where    = [];
        $keywords = $request->param("keywords");
        if($keywords){
            array_push($where,["b.username|b.email|b.phone|b.fullname",'eq',$keywords]);
        }

        $userList  = $this->model
            ->alias("a")
            ->leftJoin ("cp_admin b","a.uid=b.id")
            ->field("a.id,a.uid,b.username,a.status,a.create_time,a.update_time")
            ->where($where)
            ->group("a.uid")
            ->paginate();

        $this->assign([
            "userList" => $userList,
            "search"   => [
                "keywords" => $keywords
            ]
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="创建api用户",
     *     parent="admin/user.apiuser",
     *     MenuRoot="admin/user.index",
     *     is_menu=0
     * )
     */
    public function addapiuser(Request $request)
    {
        if($request->isAjax())
        {
            $dataType = $request->param("dataType");

            if($dataType =='user')
            {
                $userName = $request->param("username");
                $userList = (new User())->where("username|fullname|phone|email",$userName)
                    ->field("id as value,username as name")
                    ->select()
                    ->toArray();
                return json($userList);
            }
        }
        $this->add(url("admin/user.apiuser/index"));

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="编辑api用户",
     *     parent="admin/user.apiuser",
     *     MenuRoot="admin/user.index",
     *     is_menu=0
     * )
     */
    public function editapiuser(Request $request,$id=0)
    {
        $this->edit(url("admin/user.apiuser/index"));

        $user = $this->model
            ->alias("a")
            ->leftJoin ("cp_admin b","a.uid=b.id")
            ->field("a.id,a.uid,b.username,a.status")
            ->group("a.uid")
            ->where("a.id",$id)
            ->find();

        $this->assign([
            "user" => $user
        ]);

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="删除api用户",
     *     parent="admin/user.apiuser",
     *     MenuRoot="admin/user.index",
     *     is_menu=0,
     *     is_view=0
     * )
     */
    public function delapiuser(Request $request)
    {
        $this->del();
    }

}